package Text3;

import java.util.*;
public class Main {
    public static int N = 100010, n, m, idx;
    public static int[] h = new int[N];
    public static int[] e = new int[N];
    public static int[] ne = new int[N];
    public static int[] d = new int[N];

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        Arrays.fill(h,-1);
        Arrays.fill(d,-1);
        for (int i = 0; i < m; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            add(a, b);
        }
        d[0]=0;
        System.out.println(bfs());
    }

    private static int bfs() {
        Queue<Integer> q= new LinkedList<>();
        q.offer(1);
        while(!q.isEmpty()){
            int t=q.poll();
            for (int i = h[t]; i!=-1 ; i=ne[i]) {
                int j=e[i];
                if(d[j]==-1){
                    d[j]=d[t]+1;
                    q.offer(j);
                }
            }
        }
        return d[n];
    }

    public static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
